home *** CD-ROM | disk | FTP | other *** search
- Path: lerc.nasa.gov!purdue!yuma!usenet
- From: shuyuan xuan <xuan>
- Newsgroups: comp.lang.c
- Subject: why doesn't atof work properly ?
- Date: 3 Feb 1996 21:10:09 GMT
- Organization: Colorado State University, Fort Collins, CO 80523
- Message-ID: <4f0ivh$3g7e@yuma.ACNS.ColoState.EDU>
- NNTP-Posting-Host: glazunov.cs.colostate.edu
- Mime-Version: 1.0
- Content-Type: multipart/mixed;
- boundary="-------------------------------805478414378"
- X-Mailer: Mozilla 1.12 (X11; I; HP-UX A.09.01 9000/715)
- X-URL: file:/s/chopin/c/grad/xuan/c5/m.c
-
- This is a multi-part message in MIME format.
-
- ---------------------------------805478414378
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
-
- Thanx in advance.
- I was reading from da.dat, making everything into a char string.
- Then I used strtok to interpret the string, converting them into int or
- double variables through atoi or atof. But atof doesn't seem to work
- at all. Enclosed is the file, da.dat and output.
-
- Help urgently needed. I've been thinking for days.
-
- xuan
-
- ---------------------------------805478414378
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain
-
- #include "stdio.h"
- #include "strings.h"
- #include "string.h"
-
- main()
- {
- FILE * infile;
- char contents[600],line[30];
- char * s, * buf;
- int i,j,l,n;
- double num;
-
- infile=fopen ("da.dat","r");
- i= 0;
- while((fgets(line,29,infile))!=NULL && i<30)
- {
- strcat(contents,line);
- i++;
- }
-
- fclose(infile);
- j= atoi(strtok(contents," \n\t\r"));
- for (n=0; n<j; n++)
- {
- l= atoi(strtok(NULL," \n\t\r"));
- for (i=0;i<l;i++)
- {
- s=strtok(NULL," \n\t\r");
- num= atof(s);
- printf(" %lf",num);
- }
- printf("\n");
- }
- }
-
- /**** the da.dat file ******
- 4
- 4
- 0 1.1 2.2 3.3
-
- 3
- 1.0 2.0 .3
-
- 2
- 1.0 2.0
-
- 5
- 0 1.0 .2 .3 4.0
-
- ******the output **********
-
- Script started on Sat Feb 3 13:59:50 1996
- glazunov> m
-
- 0.000000 1072798105.000000 1073846681.000000 1074423398.000000
- 1072693248.000000 1073741824.000000 1070805811.000000
- 1072693248.000000 1073741824.000000
- 0.000000 1072693248.000000 1070176665.000000 1070805811.000000 1074790400.000000
- glazunov> exit
-
- glazunov>
- script done on Sat Feb 3 13:59:55 1996
- *****************************/
-
- ---------------------------------805478414378--
-